package com.nf.health.app.activity.my.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nf.health.app.R;
import com.nf.health.app.activity.my.bean.SweatQuestryModel;
import com.nf.health.app.adapter.ImageViewAdapter2;
import com.nf.health.app.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 恩福 on 2016/9/28.
 */

public class QuerySweatTrackListAdapter extends RecyclerView.Adapter<QuerySweatTrackListAdapter.MyViewHolder> {
    private Context context;
    private OnItemClickListener mOnItemClickLitener;
    private List<SweatQuestryModel> list;

    public QuerySweatTrackListAdapter(Context context, List<SweatQuestryModel> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.sweat_track_list_item, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final SweatQuestryModel  sweatQuestryModel = list.get(position);
        if(sweatQuestryModel.isOpen() == true || position ==0){
            sweatQuestryModel.setOpen(true);
            holder.upTriangle.setImageResource(R.drawable.iv_blue_arrowup);
        }
        holder.delete.setVisibility(View.GONE);
         long usertime = 0L;
        if(TextUtils.isEmpty( sweatQuestryModel.getTime())){
            usertime = System.currentTimeMillis();
        }else{
            usertime = Long.parseLong(sweatQuestryModel.getTime());
        }
        String historyTime = DateUtil.date2String(usertime,"yyyy-MM-dd hh:mm;ss");

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String time = formatter.format(curDate);
        String historyTime2 = historyTime.split(" ")[0];

        //数据如果是今天的，就让时间轴为选中状态
        if(time.equals(historyTime2)){
            holder.timeMark.setSelected(true);
        }else{
            holder.timeMark.setSelected(false);
        }
        holder.time.setText(historyTime);
        ImageViewAdapter2 imageViewAdapter = new ImageViewAdapter2(context,sweatQuestryModel);
        holder.recyclerView.setAdapter(imageViewAdapter);


        if(sweatQuestryModel.isOpen()){
            holder.upTriangle.setImageResource(R.drawable.iv_blue_arrowup);
            //显示汗液图片
            holder.sweatHistoryContent.setVisibility(View.VISIBLE);
        }else{
            holder.upTriangle.setImageResource(R.drawable.iv_blue_arrowdown);
            //隐藏图片
            holder.sweatHistoryContent.setVisibility(View.GONE);
        }

        holder.sweatHistoryDateContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sweatQuestryModel.isOpen()){
                    holder.upTriangle.setImageResource(R.drawable.iv_blue_arrowup);
                    //显示汗液图片
                    holder.sweatHistoryContent.setVisibility(View.VISIBLE);
                    sweatQuestryModel.setOpen(false);
                }else{
                    holder.upTriangle.setImageResource(R.drawable.iv_blue_arrowdown);
                    //隐藏图片
                    holder.sweatHistoryContent.setVisibility(View.GONE);
                    sweatQuestryModel.setOpen(true);
                }

            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(holder.delete,position);
                Toast.makeText(context,"delete",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView timeMark;
        RelativeLayout sweatHistoryDateContent;
        RelativeLayout sweatHistoryContent;
        TextView time;
        TextView sweat_unit;
        ImageView upTriangle;
        ImageView delete;
        RecyclerView recyclerView;

        public MyViewHolder(View itemView) {
            super(itemView);
            timeMark = (ImageView) itemView.findViewById(R.id.iv_time_mark);
            sweatHistoryDateContent = (RelativeLayout) itemView.findViewById(R.id.sweat_history_date_content);
            sweatHistoryContent = (RelativeLayout) itemView.findViewById(R.id.sweat_history_content);
            time = (TextView) itemView.findViewById(R.id.tv_date);
            sweat_unit = (TextView) itemView.findViewById(R.id.tv_sweat_unit);
            upTriangle = (ImageView) itemView.findViewById(R.id.iv_up_triangle);
            delete = (ImageView) itemView.findViewById(R.id.iv_delete);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_sweat_image);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));

            String sweatUnit = "<font color='#2E86F4'>温度单位:℃</font> &nbsp;&nbsp;&nbsp;" +
                    "<font color='#FFA500'>汗出单位:g/m³</font>";
            CharSequence charSequenced = Html.fromHtml(sweatUnit);
            sweat_unit.setText(charSequenced);
            sweat_unit.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }


    //监听事件
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


    public void setOnItemClickLitener(OnItemClickListener onItemClickLitener) {
        if(mOnItemClickLitener != null) {
            this.mOnItemClickLitener = onItemClickLitener;
        }
    }


    public void addAll(List<SweatQuestryModel> subList) {
        int lastIndex = list.size();
        if (this.list.addAll(subList)) {
            notifyItemRangeInserted(lastIndex, list.size());
        }
    }
    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

}
